home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
CTEX__
/
PXTOPK.C
< prev
Wrap
Text File
|
1987-05-25
|
19KB
|
668 lines
/* pxtopk.c by tomas rokicki */
/* comment to avoid beastly number */
#include <stdio.h>
#define maxmemsize 200000
#define namelength 80
#define terminallinelength 132
#define lo(a) ((a)&65535)
#define hi(a) lo((a)>>16)
#define incr(a) a++
#define decr(a) a--
#define true (1)
#define false (0)
#define chr(a) a
#define round(a) ((int)(a+.5))
typedef int integer ;
typedef char quarterword ;
typedef char boolean ;
typedef float real ;
typedef quarterword ASCIIcode ;
typedef FILE *textfile ;
typedef quarterword eightbits ;
typedef FILE *wordfile ;
typedef FILE *bytefile ;
ASCIIcode xord[128] ;
char xchr[256] ;
wordfile pxlfile ;
bytefile pkfile ;
char pxlname[81], pkname[81] ;
integer pkloc ;
integer bitweight ;
integer outputbyte ;
integer mem[200001] ;
integer nextmemfree ;
integer dirptr ;
integer car ;
integer hppp ;
char comment[20] ;
integer width ;
integer height ;
integer cxoff ;
integer cyoff ;
real pxlconv ;
integer power[32] ;
integer haveext ;
integer lastext ;
integer gargc ;
integer i ;
char **gargv ;
initialize () { integer i ;
printf ( "This is PXtoPK, C Version 2.1\n" ) ;
for ( i = 0 ; i <= 31 ; i ++ ) xchr [ i ] = '?' ;
xchr [ 32 ] = ' ' ;
xchr [ 33 ] = '!' ;
xchr [ 34 ] = '"' ;
xchr [ 35 ] = '#' ;
xchr [ 36 ] = '$' ;
xchr [ 37 ] = '%' ;
xchr [ 38 ] = '&' ;
xchr [ 39 ] = '\'' ;
xchr [ 40 ] = '(' ;
xchr [ 41 ] = ')' ;
xchr [ 42 ] = '*' ;
xchr [ 43 ] = '+' ;
xchr [ 44 ] = ',' ;
xchr [ 45 ] = '-' ;
xchr [ 46 ] = '.' ;
xchr [ 47 ] = '/' ;
xchr [ 48 ] = '0' ;
xchr [ 49 ] = '1' ;
xchr [ 50 ] = '2' ;
xchr [ 51 ] = '3' ;
xchr [ 52 ] = '4' ;
xchr [ 53 ] = '5' ;
xchr [ 54 ] = '6' ;
xchr [ 55 ] = '7' ;
xchr [ 56 ] = '8' ;
xchr [ 57 ] = '9' ;
xchr [ 58 ] = ':' ;
xchr [ 59 ] = ';' ;
xchr [ 60 ] = '<' ;
xchr [ 61 ] = '=' ;
xchr [ 62 ] = '>' ;
xchr [ 63 ] = '?' ;
xchr [ 64 ] = '@' ;
xchr [ 65 ] = 'A' ;
xchr [ 66 ] = 'B' ;
xchr [ 67 ] = 'C' ;
xchr [ 68 ] = 'D' ;
xchr [ 69 ] = 'E' ;
xchr [ 70 ] = 'F' ;
xchr [ 71 ] = 'G' ;
xchr [ 72 ] = 'H' ;
xchr [ 73 ] = 'I' ;
xchr [ 74 ] = 'J' ;
xchr [ 75 ] = 'K' ;
xchr [ 76 ] = 'L' ;
xchr [ 77 ] = 'M' ;
xchr [ 78 ] = 'N' ;
xchr [ 79 ] = 'O' ;
xchr [ 80 ] = 'P' ;
xchr [ 81 ] = 'Q' ;
xchr [ 82 ] = 'R' ;
xchr [ 83 ] = 'S' ;
xchr [ 84 ] = 'T' ;
xchr [ 85 ] = 'U' ;
xchr [ 86 ] = 'V' ;
xchr [ 87 ] = 'W' ;
xchr [ 88 ] = 'X' ;
xchr [ 89 ] = 'Y' ;
xchr [ 90 ] = 'Z' ;
xchr [ 91 ] = '[' ;
xchr [ 92 ] = '\\' ;
xchr [ 93 ] = ']' ;
xchr [ 94 ] = '^' ;
xchr [ 95 ] = '_' ;
xchr [ 96 ] = '`' ;
xchr [ 97 ] = 'a' ;
xchr [ 98 ] = 'b' ;
xchr [ 99 ] = 'c' ;
xchr [ 100 ] = 'd' ;
xchr [ 101 ] = 'e' ;
xchr [ 102 ] = 'f' ;
xchr [ 103 ] = 'g' ;
xchr [ 104 ] = 'h' ;
xchr [ 105 ] = 'i' ;
xchr [ 106 ] = 'j' ;
xchr [ 107 ] = 'k' ;
xchr [ 108 ] = 'l' ;
xchr [ 109 ] = 'm' ;
xchr [ 110 ] = 'n' ;
xchr [ 111 ] = 'o' ;
xchr [ 112 ] = 'p' ;
xchr [ 113 ] = 'q' ;
xchr [ 114 ] = 'r' ;
xchr [ 115 ] = 's' ;
xchr [ 116 ] = 't' ;
xchr [ 117 ] = 'u' ;
xchr [ 118 ] = 'v' ;
xchr [ 119 ] = 'w' ;
xchr [ 120 ] = 'x' ;
xchr [ 121 ] = 'y' ;
xchr [ 122 ] = 'z' ;
xchr [ 123 ] = '{' ;
xchr [ 124 ] = '|' ;
xchr [ 125 ] = '}' ;
xchr [ 126 ] = '~' ;
for ( i = 127 ; i <= 255 ; i ++ ) xchr [ i ] = '?' ;
for ( i = 0 ; i <= 127 ; i ++ ) xord [ chr ( i ) ] = 32 ;
for ( i = 32 ; i <= 126 ; i ++ ) xord [ xchr [ i ] ] = i ;
pkloc = 0 ;
strcpy(comment , " PXTOPK 2.1 output" ) ;
power [ 0 ] = 1 ;
for ( i = 1 ; i <= 30 ; i ++ ) power [ i ] = power [ i - 1 ] * 2 ;
power [ 31 ] = - power [ i - 1 ] - power [ i - 1 ] ;
}
jumpout () { exit ( 1 ) ;
}
openpxlfile () { pxlfile = fopen ( pxlname , "r" ) ;
}
openpkfile () { pkfile = fopen ( pkname , "w" ) ;
}
integer pixelinteger () { integer i ;
i = getc ( pxlfile ) ;
if ( i > 127 ) i = i - 256 ;
i = i * 256 + getc ( pxlfile ) ;
i = i * 256 + getc ( pxlfile ) ;
i = i * 256 + getc ( pxlfile ) ;
return ( i ) ;
}
pkbyte ( b )
integer b ;
{ if ( b < 0 ) b = b + 256 ;
putc ( b , pkfile ) ;
incr ( pkloc ) ;
}
pkhalfword ( a )
integer a ;
{ if ( a < 0 ) a = a + 65536 ;
pkbyte ( a / 256 ) ;
pkbyte ( a % 256 ) ;
}
pkthreebytes ( a )
integer a ;
{ pkbyte ( a / 65536 % 256 ) ;
pkbyte ( a / 256 % 256 ) ;
pkbyte ( a % 256 ) ;
}
pkword ( a )
integer a ;
{ integer b ;
if ( a < 0 )
{ a = a + ( 1073741824 ) + ( 1073741824 ) ;
b = 128 + a / 16777216 ;
}
else b = a / 16777216 ;
pkbyte ( b ) ;
pkbyte ( a / 65536 % 256 ) ;
pkbyte ( a / 256 % 256 ) ;
pkbyte ( a % 256 ) ;
}
pknyb ( a )
integer a ;
{ if ( bitweight == 16 )
{ outputbyte = a * 16 ;
bitweight = 1 ;
}
else
{ pkbyte ( outputbyte + a ) ;
bitweight = 16 ;
}
}
loadpxlfile () { /* 9997 9999 */ integer k ;
openpxlfile () ;
k = 0 ;
if ( feof(pxlfile) ) goto lab9997 ;
while ( ! feof(pxlfile) )
{ mem [ k ] = pixelinteger () ;
incr ( k ) ;
if ( k > maxmemsize )
{ printf ( " PXtoPK memory size exceeded on load of pixel file!\n" ) ;
jumpout () ;
}
}
k-- ;
printf ( "%d bytes read from pixel file.\n" , ( 4 * k ) ) ;
if ( k + 10000 > maxmemsize )
{ printf ( " I don't think that there will be enough memory.\n"
) ;
jumpout () ;
}
nextmemfree = k ;
k = k - 1 ;
if ( ( mem [ k ] != 1001 ) || ( mem [ 0 ] != 1001 ) ) goto lab9997 ;
goto lab9999 ;
lab9997 :
{ printf ( " PXL file is bad\n" ) ;
jumpout () ;
}
lab9999 : dirptr = mem [ k - 1 ] ;
}
integer los ( a )
integer a ;
{ integer b ;
b = lo ( a ) ;
if ( b > 32767 ) return ( b - 65536 ) ;
else return ( b ) ;
}
writepreamble () { integer i ;
openpkfile () ;
pkbyte ( 247 ) ;
pkbyte ( 89 ) ;
pkbyte ( 17 ) ;
for ( i = 1 ; i <= 17 ; i ++ ) pkbyte ( xord [ comment [ i ] ] ) ;
pkword ( mem [ dirptr + 514 ] ) ;
pkword ( mem [ dirptr + 512 ] ) ;
pkword ( hppp ) ;
pkword ( hppp ) ;
}
writepostamble () { pkbyte ( 245 ) ;
while ( ( pkloc % 4 != 0 ) ) pkbyte ( 246 ) ;
printf ( "%d bytes written to packed file.\n" , pkloc ) ;
}
boolean equal ( row1 , row2 )
integer row1 , row2 ;
{ integer i ;
boolean temp ;
i = width ;
temp = true ;
while ( ( i > 0 ) && temp )
{ if ( mem [ row1 ] != mem [ row2 ] ) temp = false ;
incr ( row1 ) ;
incr ( row2 ) ;
i = i - 32 ;
}
return ( temp ) ;
}
shipcharacter () { integer craster ;
integer wordwidth ;
integer compsize ;
integer horesc ;
integer i, j, k ;
integer zerorow ;
integer onesrow ;
integer repeatpointer ;
integer bitcounts ;
boolean bitssmaller ;
integer finalsize ;
integer count ;
integer test ;
integer curptr ;
integer bit ;
integer repeatflag ;
integer word ;
integer bitptr ;
integer bitmod32 ;
integer currepeat ;
integer endraster ;
integer dynf ;
integer deriv[14] ;
integer bcompsize ;
boolean firston ;
integer flagbyte ;
boolean state ;
boolean on ;
integer hbit ;
integer pbit ;
boolean ron, son ;
integer rcount, scount ;
integer ri, si ;
integer max2 ;
integer predpkloc ;
integer buff ;
horesc = round ( pxlconv * mem [ dirptr + 3 ] ) ;
width = hi ( mem [ dirptr ] ) ;
height = lo ( mem [ dirptr ] ) ;
cxoff = hi ( mem [ dirptr + 1 ] ) ;
if (cxoff > 32767) cxoff -= 65536 ;
cyoff = los ( mem [ dirptr + 1 ] ) ;
craster = mem [ dirptr + 2 ] ;
wordwidth = ( width + 31 ) / 32 ;
zerorow = nextmemfree ;
onesrow = nextmemfree + wordwidth ;
repeatpointer = onesrow + wordwidth ;
bitcounts = repeatpointer + height + 1 ;
for ( i = zerorow ; i <= onesrow - 1 ; i ++ ) mem [ i ] = 0 ;
for ( i = onesrow ; i <= repeatpointer - 2 ; i ++ ) mem [ i ] = - 1 ;
i = width % 32 ;
if ( i == 0 ) mem [ repeatpointer - 1 ] = - 1 ;
else if ( i == 1 ) mem [ repeatpointer - 1 ] = - ( 1073741824 ) - (
1073741824 ) ;
else mem [ repeatpointer - 1 ] = - power [ 32 - i ] ;
i = 0 ;
j = height ;
while ( i < j )
{ if ( equal ( i * wordwidth + craster , zerorow ) ) mem [
repeatpointer + i ] = 0 ;
else if ( equal ( i * wordwidth + craster , onesrow ) ) mem [
repeatpointer + i ] = 0 ;
else if ( i + 1 == j ) mem [ repeatpointer + i ] = 0 ;
else if ( equal ( i * wordwidth + craster , ( i + 1 ) * wordwidth +
craster ) ) mem [ repeatpointer + i ] = 1 ;
else mem [ repeatpointer + i ] = 0 ;
incr ( i ) ;
}
i = 0 ;
while ( i < j )
{ k = i ;
while ( mem [ repeatpointer + k ] == 1 ) incr ( k ) ;
mem [ repeatpointer + i ] = k - i ;
i = k + 1 ;
}
mem [ repeatpointer + i ] = 0 ;
repeatflag = 0 ;
bitptr = width - 1 ;
currepeat = repeatpointer ;
endraster = craster + height * wordwidth ;
curptr = bitcounts ;
count = 0 ;
test = 0 ;
do { incr ( bitptr ) ;
if ( bitptr == width )
{ bitmod32 = 0 ;
bitptr = 0 ;
if ( mem [ currepeat ] > 0 )
{ repeatflag = mem [ currepeat ] ;
currepeat = currepeat + repeatflag ;
craster = craster + wordwidth * repeatflag ;
}
incr ( currepeat ) ;
}
decr ( bitmod32 ) ;
if ( bitmod32 == - 1 )
{ bitmod32 = 31 ;
word = mem [ craster ] ;
incr ( craster ) ;
}
if ( craster > endraster ) bit = 2 ;
else if ( bitmod32 == 31 )
{ if ( word < 0 )
{ bit = 1 ;
word = word + ( 1073741824 ) + ( 1073741824 ) ;
}
else bit = 0 ;
}
else
{ if ( word >= power [ bitmod32 ] )
{ word = word - power [ bitmod32 ] ;
bit = 1 ;
}
else bit = 0 ;
}
if ( bit == test ) incr ( count ) ;
else
{ mem [ curptr ] = count ;
incr ( curptr ) ;
if ( curptr + 3 > maxmemsize )
{ printf ( " Out of memory while saving character counts!\n" ) ;
jumpout () ;
}
count = 1 ;
test = bit ;
if ( repeatflag > 0 )
{ mem [ curptr ] = - repeatflag ;
repeatflag = 0 ;
incr ( curptr ) ;
}
}
} while ( ! ( test == 2 ) ) ;
mem [ curptr ] = 0 ;
mem [ curptr + 1 ] = 0 ;
for ( i = 1 ; i <= 13 ; i ++ ) deriv [ i ] = 0 ;
firston = mem [ bitcounts ] == 0 ;
if ( firston ) incr ( bitcounts ) ;
i = bitcounts ;
compsize = 0 ;
while ( mem [ i ] != 0 )
{ j = mem [ i ] ;
if ( j == - 1 ) incr ( compsize ) ;
else
{ if ( j < 0 )
{ incr ( compsize ) ;
j = - j ;
}
if ( j < 209 ) compsize = compsize + 2 ;
else
{ k = j - 193 ;
while ( k >= 16 )
{ k = k / 16 ;
compsize = compsize + 2 ;
}
compsize = compsize + 1 ;
}
if ( j < 14 ) decr ( deriv [ j ] ) ;
else if ( j < 209 ) incr ( deriv [ ( 223 - j ) / 15 ] ) ;
else
{ k = 16 ;
while ( ( k * 16 < j + 3 ) ) k = k * 16 ;
if ( j - k <= 192 ) deriv [ ( 207 - j + k ) / 15 ] = deriv [ (
207 - j + k ) / 15 ] + 2 ;
}
}
incr ( i ) ;
}
bcompsize = compsize ;
dynf = 0 ;
for ( i = 1 ; i <= 13 ; i ++ )
{ compsize = compsize + deriv [ i ] ;
if ( compsize <= bcompsize )
{ bcompsize = compsize ;
dynf = i ;
}
}
compsize = ( bcompsize + 1 ) / 2 ;
if ( ( compsize > ( height * width + 7 ) / 8 ) || ( height * width == 0 )
)
{ compsize = ( height * width + 7 ) / 8 ;
dynf = 14 ;
}
flagbyte = dynf * 16 ;
if ( firston ) flagbyte = flagbyte + 8 ;
if ( ( mem [ dirptr + 3 ] > 16777215 ) || ( mem [ dirptr + 3 ] < 0 ) || (
horesc < 0 ) || ( compsize > 196579 ) || ( width > 65535 ) || ( height >
65535 ) || ( cxoff > 32767 ) || ( cyoff > 32767 ) || ( cxoff < - 32768 )
|| ( cyoff < - 32768 ) )
{ flagbyte = flagbyte + 7 ;
pkbyte ( flagbyte ) ;
compsize = compsize + 28 ;
pkword ( compsize ) ;
pkword ( car ) ;
predpkloc = pkloc + compsize ;
pkword ( mem [ dirptr + 3 ] ) ;
pkword ( horesc * 65536 ) ;
pkword ( 0 ) ;
pkword ( width ) ;
pkword ( height ) ;
pkword ( cxoff ) ;
pkword ( cyoff ) ;
}
else if ( ( horesc > 255 ) || ( width > 255 ) || ( height > 255 ) || (
cxoff > 127 ) || ( cyoff > 127 ) || ( cxoff < - 128 ) || ( cyoff < - 128
) || ( compsize > 1016 ) )
{ compsize = compsize + 13 ;
flagbyte = flagbyte + compsize / 65536 + 4 ;
pkbyte ( flagbyte ) ;
pkhalfword ( compsize % 65536 ) ;
pkbyte ( car ) ;
predpkloc = pkloc + compsize ;
pkthreebytes ( mem [ dirptr + 3 ] ) ;
pkhalfword ( horesc ) ;
pkhalfword ( width ) ;
pkhalfword ( height ) ;
pkhalfword ( cxoff ) ;
pkhalfword ( cyoff ) ;
}
else
{ compsize = compsize + 8 ;
flagbyte = flagbyte + compsize / 256 ;
pkbyte ( flagbyte ) ;
pkbyte ( compsize % 256 ) ;
pkbyte ( car ) ;
predpkloc = pkloc + compsize ;
pkthreebytes ( mem [ dirptr + 3 ] ) ;
pkbyte ( horesc ) ;
pkbyte ( width ) ;
pkbyte ( height ) ;
pkbyte ( cxoff ) ;
pkbyte ( cyoff ) ;
}
if ( dynf != 14 )
{ bitweight = 16 ;
max2 = 208 - 15 * dynf ;
i = bitcounts ;
while ( mem [ i ] != 0 )
{ j = mem [ i ] ;
if ( j == - 1 ) pknyb ( 15 ) ;
else
{ if ( j < 0 )
{ pknyb ( 14 ) ;
j = - j ;
}
if ( j <= dynf ) pknyb ( j ) ;
else if ( j <= max2 )
{ j = j - dynf - 1 ;
pknyb ( j / 16 + dynf + 1 ) ;
pknyb ( j % 16 ) ;
}
else
{ j = j - max2 + 15 ;
k = 16 ;
while ( k <= j )
{ k = k * 16 ;
pknyb ( 0 ) ;
}
while ( k > 1 )
{ k = k / 16 ;
pknyb ( j / k ) ;
j = j % k ;
}
}
}
incr ( i ) ;
}
if ( bitweight != 16 ) pkbyte ( outputbyte ) ;
}
else
{ buff = 0 ;
pbit = 8 ;
i = bitcounts ;
hbit = width ;
on = ! firston ;
state = false ;
count = 0 ;
repeatflag = 0 ;
while ( ( mem [ i ] != 0 ) || state || ( count > 0 ) )
{ if ( state )
{ count = rcount ;
i = ri ;
on = ron ;
decr ( repeatflag ) ;
}
else
{ rcount = count ;
ri = i ;
ron = on ;
}
do { if ( count == 0 )
{ if ( mem [ i ] < 0 )
{ if ( ! state ) repeatflag = - mem [ i ] ;
incr ( i ) ;
}
count = mem [ i ] ;
incr ( i ) ;
on = ! on ;
}
if ( ( count >= pbit ) && ( pbit < hbit ) )
{ if ( on ) buff = buff + power [ pbit ] - 1 ;
pkbyte ( buff ) ;
buff = 0 ;
hbit = hbit - pbit ;
count = count - pbit ;
pbit = 8 ;
}
else if ( ( count < pbit ) && ( count < hbit ) )
{ if ( on ) buff = buff + power [ pbit ] - power [ pbit - count ]
;
pbit = pbit - count ;
hbit = hbit - count ;
count = 0 ;
}
else
{ if ( on ) buff = buff + power [ pbit ] - power [ pbit - hbit ]
;
count = count - hbit ;
pbit = pbit - hbit ;
hbit = width ;
if ( pbit == 0 )
{ pkbyte ( buff ) ;
buff = 0 ;
pbit = 8 ;
}
}
} while ( ! ( hbit == width ) ) ;
if ( state && ( repeatflag == 0 ) )
{ count = scount ;
i = si ;
on = son ;
state = false ;
}
else if ( ! state && ( repeatflag > 0 ) )
{ scount = count ;
si = i ;
son = on ;
state = true ;
}
}
if ( pbit != 8 ) pkbyte ( buff ) ;
}
if ( predpkloc != pkloc )
{ printf ( " Bad predicted character length: character %d\n" , car ) ;
jumpout () ;
}
}
dialog () { integer i ;
if ( ( gargc < 2 ) || ( gargc > 3 ) )
{ printf ( " Usage: pxtopk pxlfile[.pxl] [pkfile[.pk]]\n" ) ;
jumpout () ;
}
strcpy ( pxlname , gargv [ 1 ] ) ;
lastext = - 1 ;
i = 0 ;
while ( pxlname [ ++ i ] != 0 ) if ( pxlname [ i ] == '.' ) lastext = i ;
else if ( pxlname [ i ] == '/' ) lastext = - 1 ;
if ( lastext == - 1 )
{ strcpy ( pxlname + i , ".pxl" ) ;
lastext = i ;
}
if ( gargc == 3 )
{ strcpy ( pkname , gargv [ 2 ] ) ;
haveext = true ;
}
else
{ strcpy ( pkname , pxlname ) ;
haveext = false ;
}
}
main (argc, argv)
int argc ;
char *argv[] ;
{ gargc = argc ;
gargv = argv ;
initialize () ;
dialog () ;
loadpxlfile () ;
pxlconv = ( mem [ dirptr + 514 ] / 16 ) / 65536.0 * mem [ dirptr + 513 ] /
72.27 / 5242880.0 ;
hppp = round ( mem [ dirptr + 513 ] * 65536 / 72.27 / 5 ) ;
i = round ( hppp * 72.27 / 65536 ) ;
if ( ! haveext ) sprintf ( pkname + lastext , ".%dpk" , i ) ;
writepreamble () ;
for ( car = 0 ; car <= 127 ; car ++ )
{ if ( mem [ dirptr + 2 ] != 0 ) shipcharacter () ;
dirptr = dirptr + 4 ;
}
writepostamble () ;
lab9999 : ;
}